package com.spsy.orders.controller;

import com.alibaba.fastjson2.JSON;
import com.spsy.common.core.domain.R;
import com.spsy.orders.domain.Orders;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author: liuzhiping
 * @date: 2025/2/17 09:24
 **/
@RestController
@RequestMapping("/orders/redisson")
public class RedissonController {


    @Autowired
    private RedissonClient redissonClient;



    @GetMapping("/{goods}")
    public R getOrders(@PathVariable String goods)
    {
        String lockRedisKey = "lock:"+goods;//a100

        System.out.println("lockRedisKey="+lockRedisKey);

        RLock rLock = this.redissonClient.getLock(lockRedisKey);

        try{
            boolean boo = rLock.tryLock(30,30, TimeUnit.SECONDS);
            System.out.println("锁定30秒 boo:"+boo);

            Thread.sleep(20000);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            rLock.unlock();
        }
        return R.ok();
    }
}
